/*
 * Name        : HttpEngine.java
 * Classes     : HttpEngine
 * Version     : 1.0
 * Date        : 2013-08-29
 *
 * Copyright 2013 CMCC.  All rights reserved.
 * This material, including documentation and any related computer
 * programs, is protected by copyright controlled by CMCC.  All
 * rights are reserved.  Copying, including reproducing, storing,
 * adapting or translating, any or all of this material requires the
 * prior written consent of CMCC.  This material also contains
 * confidential information which may not be disclosed to others
 * without the prior written consent of CMCC.
 */

package com.xgkj.diyiketang.http;

import java.lang.ref.WeakReference;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;


/**
 * class name HttpEngine.java
 * author renhaibin
 * date 2014-11-24 下午3:05:00
 *
 */
public abstract class HttpEngine {

    /**
     * Send GET request to url.
     *
     * @param url The request URL
     * @param headParams The HTTP header parameter for request
     */
    public abstract void sendGet(String url, Map<String ,String> headParams);

    /**
     * Send POST request to url.
     *
     * @param url The request URL
     * @param data The POST data
     * @param formParams The form parameter for request.
     * @param headParams The HTTP header parameter for request
     */
    public abstract void sendPost(String url, byte[] data, Map<String, String> formParams ,Map<String ,String> headParams);

    /**
     * Set connection time out
     *
     * @param timeOut Time out value in ms
     */
    public abstract void setConnectionTimeOut(int timeOut);
    

    private List<WeakReference<HttpEventListener>> mListenerList = new LinkedList<WeakReference<HttpEventListener>>();

    /**
     * Add HttpEventListener to handle http event.
     * @param listener The listener need be added
     */
    public void addHttpListener(HttpEventListener listener) {
        if (listener == null) {
            return;
        }

        if (findListener(listener) == null) {
            mListenerList.add(new WeakReference<HttpEventListener>(listener));
        }
    }

    /**
     * Remove HttpEventListener from listener list.
     * @param listener The listener need be removed
     */
    public void removeHttpListener(HttpEventListener listener) {
        if (listener == null) {
            return;
        }

        WeakReference<HttpEventListener> ref = findListener(listener);
        if (ref != null) {
            mListenerList.remove(ref);
        }
    }

    /**
     * Notify all listeners when HttpEvent is fetched.
     * @param event The http event
     */
    protected void notifyListener(HttpEvent event) {
        for (WeakReference<HttpEventListener> ref : mListenerList) {
            if (ref != null) {
                HttpEventListener listener = ref.get();
                if (listener != null) {
                    listener.handleHttpEvent(event);
                }
            }
        }
    }

    private WeakReference<HttpEventListener> findListener(HttpEventListener listener) {
        if (mListenerList == null) {
            return null;
        }

        for (WeakReference<HttpEventListener> ref : mListenerList) {
            if (ref.get() == listener) {
                return ref;
            }
        }

        return null;
    }
}
